package com.yangchoi.jetpackcompose.ui.widget.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import com.yangchoi.jetpackcompose.theme.c94_12sp

@Composable
fun AutoSizingTextField(
    modifier: Modifier = Modifier,
    hintModifier: Modifier = Modifier,
    value: String = "",
    hintStr:String = "请输入内容",
    hintStrTextStyle: TextStyle = c94_12sp,
    onValueChange: (String) -> Unit,
    textStyle: TextStyle = LocalTextStyle.current,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,// 键盘 输入类型
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    maxLines: Int = Int.MAX_VALUE,
    maxLength: Int = Int.MAX_VALUE,
    singleLine: Boolean = false,
    needShowCenter: Boolean = false,
    bgColor: Color = Color.Transparent,
    shape: RoundedCornerShape = RoundedCornerShape(0.dp),
    showPassword:Boolean = true,
    visualTransformation: VisualTransformation = VisualTransformation.None,
) {
    if(singleLine){
        LazyRow(horizontalArrangement = if(needShowCenter) Arrangement.Center else Arrangement.Start) {
            item {
                BasicTextField(
                    modifier = modifier
                        .width(IntrinsicSize.Max),
                    value = value,
                    onValueChange = {
                        if(value.length <= maxLength){
                            onValueChange(it)
                        }
                    },
                    textStyle = textStyle,
                    keyboardOptions = keyboardOptions,
                    keyboardActions = keyboardActions,
                    maxLines = 1,
                    singleLine = true,
                    decorationBox = { innerTextField ->
                        if (value.isEmpty()) Text(text = hintStr, style = hintStrTextStyle,modifier = hintModifier)
                        innerTextField()
                    },
                    visualTransformation = if (showPassword) visualTransformation else PasswordVisualTransformation()
                )
            }
        }
    }else{
        var height by remember { mutableStateOf(0) }
        BasicTextField(
            modifier = modifier.height(height.dp),
            value = value,
            onValueChange = {
                if(value.length <= maxLength){
                    onValueChange(it)
                }
            },
            textStyle = textStyle,
            keyboardOptions = keyboardOptions,
            keyboardActions = keyboardActions,
            maxLines = maxLines,
            onTextLayout = { textLayoutResult ->
                height = textLayoutResult.size.height
            },
            decorationBox = { innerTextField ->
                Box(modifier = Modifier
                    .padding(start = 4.dp, end = 4.dp)
                    .background(color = bgColor, shape = shape)
                ) {
                    if (value.isEmpty()) Text(text = hintStr, style = hintStrTextStyle,modifier = hintModifier)
                    innerTextField()
                }
            },
        )
    }
}